// pages/constmer/user/index.js
var api = require("../../../../api.js"),
  QQMapWX = require("../../../../qqmap-wx.js"),
  app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNumber: '',
    sharePosterPanel: false,
    pic:'',
  },
  navToUser: function(){
    wx.navigateTo({
      url: '/sd_liferuning/pages/constmer/user-info/index',
    })
  },
  callPhone: function () {
    var that = this;
    app.request({
      url: api.default.getPhone,
      data:{
        bid: wx.getStorageSync('bid')
      },
      success: function (res) {

        that.setData({
          phoneNumber: res.data.phone
        });
        wx.makePhoneCall({
          phoneNumber: that.data.phoneNumber,
        })
      }
    })
  },
  //自定义底部跳转
  naveClick: function (t) {
    app.navigatorClick(t, this);
  },
  onShow:function(){
    var that = this
    that.setData({
      head: wx.getStorageSync('head'),
      nickname: wx.getStorageSync('nickname'),
      site: wx.getStorageSync('site')
    })
    //个人中心
    var o = wx.getStorageSync('member');
    o ? that.setData(o) : wx.showLoading({
      title: '请稍后',
    });

   that.member();
  },
  member:function(){
    var that = this
    var mem={};
    app.request({
      url: api.user.UserMember,
      data: {
        bid: wx.getStorageSync('bid')
      },
      success: function (res) {
        /** 模块申请状态 */
        app.request({
          url: api.order.Is_ShenHe,
          data: {
            uid: wx.getStorageSync('uid'),
            bid: wx.getStorageSync('bid')
          },
          success: function (data) {
            that.setData({
              is_status: data.code
            })
            if (data.code == 0) {
              that.setData({
                userConfig: data.data
              });
              mem.userConfig = data.data
              wx.setStorageSync('member', mem)
            }else if (data.code == 99) {
              that.setData({
                userConfig: data.data
              });
              mem.userConfig = data.data
              wx.setStorageSync('member', mem)
            }else{
              mem.is_status = data.code
              that.setData({
                userConfig: res.data
              });
              mem.userConfig = res.data
              wx.setStorageSync('member', mem)
            }
          }
        })
      }
    })

    app.request({
      url: api.user.membertag,
      data: {
        uid: wx.getStorageSync('uid'),
        bid: wx.getStorageSync('bid')
      },
      success: function (e) {
        that.setData({
          tag: e.data
        })
        mem.tag = e.data
        wx.setStorageSync('member', mem)
      }
    })


    app.request({
      url: api.member.show,
      data: {
        uid: wx.getStorageSync('uid')
      },
      method: 'post',
      success: function (res) {


        var pic = res.data
  
        
      
        if (res.code == 100) {
          // 非会员用户
        } else {
          var pic = res.data
          that.setData({
            pic: pic,
          })
          mem.pic = res.data
          wx.setStorageSync('member', mem)
        }

      }
    })
    wx.hideLoading()
  },
  onLoad:function(){
    app.pageOnLoad(this);
    var that = this
    that.setData({
      icons: wx.getStorageSync('site') + '/addons/sd_liferuning/tp/public/uploads/background',

    });
    // wx.request({
    //   url: api.default.adimg,
    //   data: {
    //     bid: wx.getStorageSync('bid'),
    //   },
    //   success: function (res) {
    //     let imgUrl = '';
    //     imgUrl = res.data.poster;
    //     that.setData({
    //       shareImgUrl: imgUrl,
    //     })
    //   }
    // })
  },
  /**
   * 打开分享面板
   */
  openSharePosterPanel: function () {
    var that=this;
    let sharePosterPanel = that.data.sharePosterPanel;
    wx.showLoading({
      title: '请稍后',
      success:function(){
        app.request({
          url: api.user.qrcode,
          data: {
            uid: wx.getStorageSync('uid'),
            bid: wx.getStorageSync('bid')
          },
          success: function (e) {
            if(e.code==1){
              wx.hideLoading();
              that.setData({
                sharePosterPanel: true,
                qrcode:e.src
              });
            }else{
              wx.hideLoading();
              wx.showToast({
                title: '请重新点击生成',
                duration:1000,
                icon:'none'
              })
            }
          }
        })
      }
    })
   
  },
  /**
   * 关闭分享面板
   */
  closeSharePosterPanel: function(){
    let sharePosterPanel = this.data.sharePosterPanel;
    this.setData({
      sharePosterPanel: false
    });
  },
  /**
   * 保存图片
   */
  saveImage: function(e){
    let _this = this;
    let _url = e.currentTarget.dataset.url;
    wx.downloadFile({
      url: _url, 
      success: function (res) {
        if (res.statusCode === 200) {
          let tempFilePath = res.tempFilePath;
          // 存储图片
          wx.saveImageToPhotosAlbum({
            filePath: tempFilePath,
            success: function () {
              wx.showToast({
                icon: "none",
                title: '保存成功',
              })
            },
            complete: function () {
              _this.closeSharePosterPanel();
            }
          })
        }
      }
    })
  }
  
})